using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantSubmarineInput
/// \brief  The submarine input wrapper
[AddComponentMenu("GK/AbyssalGiant/Submarine/gkAbyssalGiantSubmarineInput")]
public class gkAbyssalGiantSubmarineInput : MonoBehaviour
{
    /// The maximum tilt rotation in degree
    public float MaxTiltInDegree = 20.0f;

    /// The current tilt in percent of the total allowed amplitude
    private Vector3 m_f3TiltPercent = new Vector3(0, 0, 0);
	
	/// Is the boost button pressed ?
	private bool m_bBoost;
	
	/// Is the slow motion button pressed ?
	private bool m_bSlowMotion;
	
	/// \brief  Is the boost activated?
	public bool IsBoost()
	{
		return m_bBoost;
	}
	
	/// \brief  Is the slow motion activated?
	public bool IsSlowMotion()
	{
		return m_bSlowMotion;
	}
	
    /// \brief  Get the tilt percent
    public Vector3 GetTiltPercent()
    {
        return m_f3TiltPercent;
    }

    /// \brief  Called each frame
    void Update()
    {
        Vector3 f3DeviceAttitudeRemap = new Vector3(0,0,0);

        // If there is an accelerometer consider the attitude
        f3DeviceAttitudeRemap.x = Input.acceleration.z;
        f3DeviceAttitudeRemap.y = Input.acceleration.x;
        f3DeviceAttitudeRemap.z = 0.0f;

        // Scale the tilt in the range [-m_fMaxTiltInDegree, m_fMaxTiltInDegree]
        m_f3TiltPercent = (f3DeviceAttitudeRemap*(180.0f/Mathf.PI))/MaxTiltInDegree;

        // Else take a stick
        m_f3TiltPercent.x = Input.GetAxis("Horizontal");
        m_f3TiltPercent.y = Input.GetAxis("Vertical");

        // Clamp
        // X
        if(m_f3TiltPercent.x > 1.0f)
        {
	        m_f3TiltPercent.x = 1.0f;
        }
        else if(m_f3TiltPercent.x < -1.0f)
        {
	        m_f3TiltPercent.x = -1.0f;
        }

        // Y
        if(m_f3TiltPercent.y > 1.0f)
        {
	        m_f3TiltPercent.y = 1.0f;
        }
        else if(m_f3TiltPercent.y < -1.0f)
        {
	        m_f3TiltPercent.y = -1.0f;
        }
		
		// Boost
		m_bBoost = Input.GetButton("Boost");
		
		// Slow motion
		m_bSlowMotion = Input.GetButton("SlowMotion");
    }
}
